package com.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.service.impl.*;
import com.cw.common.api.Response;
import com.cw.common.api.ResultObject;
import com.cw.common.constant.*;
import com.cw.entity.*;
import com.cw.mapper.*;
import com.cw.mapper2.*;
import com.cw.service.*;
import com.cw.util.*;
import java.time.*;
import java.util.*;
import javax.annotation.*;
import org.apache.commons.collections4.*;
import org.springframework.stereotype.*;

/**
 * <p>
 * 床位、病人 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-27
 */
@Service
public class PatientBedServiceImpl extends ServiceImpl<PatientBedMapper, PatientBed> implements IPatientBedService {

    @Resource
    private PatientInfoMapper patientInfoMapper;

    @Resource
    private ThisIiPrepayinMapper thisIiPrepayinMapper;

    @Resource
    private BedMapper bedMapper;

    /**
     * 根据床位id获取病人就诊号
     *
     * @param bedId
     * @return
     */
    @Override
    public PatientBed getVisitNo(String bedId) {
	  PatientBed patientBed = new PatientBed();
	  List<PatientBed> patients = baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).eq(PatientBed::getBedId, bedId)
																    .eq(PatientBed::getDeleteFlag, "0"));
	  if (CollectionUtils.isNotEmpty(patients)) {
		patientBed = patients.get(0);
	  }
	  return patientBed;
    }

    /**
     * 根据床位ids已登记床位
     *
     * @param bedIds
     * @return
     */
    @Override
    public List<PatientBed> getRegisterBedByBedIds(List<String> bedIds) {
	  return baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).in(PatientBed::getBedId, bedIds).eq(PatientBed::getDeleteFlag, "0"));
    }

	/**
     * 床位登记
     *
     * @param bedId
     * @param patientId
     * @return
     */
    @Override
    public Response addPatientBed(String bedId, String patientId) {

//	  String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
	  String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
	  //根据病人ID获取就诊号
	  PatientInfo patientInfo = patientInfoMapper.selectById(patientId);
	  PatientBed patientBed = new PatientBed();
	  patientBed.setId(UUIDUtil.uuid());
	  patientBed.setBedId(bedId);
	  patientBed.setPatientId(patientId);
	  patientBed.setVisitTypeCode(patientInfo.getVisitTypeCode());
	  patientBed.setVisitTypeName(patientInfo.getVisitTypeName());
	  patientBed.setVisitNo(patientInfo.getVisitNo());
	  patientBed.setCreateBy(userId);
//	  patientBed.setCreateTime(new Date());
	  patientBed.setCreateTime(LocalDateTime.now());
	  patientBed.setIsHisConfirm("1");
	  Integer insert = baseMapper.insert(patientBed);
	  //获取床位信息
	  Bed bed = bedMapper.selectById(bedId);
	  if (bed == null) {
		return new Response(500, "床位登记失败");
	  }
	  bed.setIsEmptyCode("1");
	  bed.setIsEmptyName("非空");
	  Integer update = bedMapper.updateById(bed);
	  if (insert > 0 && update > 0) {
		return new Response(200, "床位登记成功");
	  } else {
		return new Response(500, "床位登记失败");
	  }
    }


    /**
     * 转床
     *
     * @param bedId
     * @param patientId
     * @return
     */
    @Override
    public synchronized Response changePatientBed(String bedId, String patientId) {
	  //获取该用户原来床位信息
	  List<PatientBed> patientBedList = baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).eq(PatientBed::getPatientId, patientId)
																	    .eq(PatientBed::getDeleteFlag, "0")
																	    .eq(PatientBed::getIsHisConfirm, "1"));
	  if (CollectionUtils.isEmpty(patientBedList)) {
		return new Response(500, "转床失败");
	  }
	  //删除原来的登记信息
	  PatientBed patientBed = patientBedList.get(0);
	  patientBed.setDeleteFlag("0");
	  Integer update = baseMapper.updateById(patientBed);
	  Bed bed = bedMapper.selectById(patientBed.getBedId());
	  bed.setIsEmptyCode("0");
	  bed.setIsEmptyName("非空");
	  Integer update1 = bedMapper.updateById(bed);
	  //重新登记新床位
	  String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
	  //根据病人ID获取就诊号
	  PatientInfo patientInfo = patientInfoMapper.selectById(patientId);
	  PatientBed patientBedNew = new PatientBed();
	  patientBedNew.setId(UUIDUtil.uuid());
	  patientBedNew.setBedId(bedId);
	  patientBedNew.setPatientId(patientId);
	  patientBedNew.setVisitTypeCode(patientInfo.getVisitTypeCode());
	  patientBedNew.setVisitTypeName(patientInfo.getVisitTypeName());
	  patientBedNew.setVisitNo(patientInfo.getVisitNo());
	  patientBedNew.setCreateBy(userId);
//	  patientBed.setCreateTime(new Date());
	  patientBedNew.setCreateTime(LocalDateTime.now());
	  patientBedNew.setIsHisConfirm("1");
	  Integer insert = baseMapper.insert(patientBedNew);
	  //获取床位信息
	  Bed bedNew = bedMapper.selectById(bedId);
	  if (bedNew == null) {
		return new Response(500, "转床失败");
	  }
	  bedNew.setIsEmptyCode("1");
	  bedNew.setIsEmptyName("非空");
	  Integer update2 = bedMapper.updateById(bedNew);
	  if (insert > 0 && update > 0 && update1 > 0 && update2 > 0) {
		return new Response(200, "转床成功");
	  } else {
		return new Response(500, "转床失败");
	  }
    }

    /**
     * 床位登记
     *
     * @param bedId
     * @param patientId
     * @return
     */
    @Override
    public synchronized Response addPatientBed1(String bedId, String patientId) {
	  //判断是否被登记
	  List<PatientBed> patientBedNew = baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).eq(PatientBed::getBedId, bedId)
																	   .eq(PatientBed::getIsHisConfirm, "1")
																	   .eq(PatientBed::getDeleteFlag, "0"));
	  if (CollectionUtils.isNotEmpty(patientBedNew)) {
		return new Response(500, "床位登记失败");
	  }
//	  String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
	  String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
	  //根据病人ID获取就诊号
	  ThisIiPrepayin patientInfo = thisIiPrepayinMapper.selectById(patientId);
	  PatientBed patientBed = new PatientBed();
	  patientBed.setId(UUIDUtil.uuid());
	  patientBed.setBedId(bedId);
	  patientBed.setPatientId(patientId);
//	  patientBed.setVisitTypeCode(patientInfo.getVisitTypeCode());
//	  patientBed.setVisitTypeName(patientInfo.getVisitTypeName());
//	  patientBed.setVisitNo(patientInfo.getPatientNo());
	  patientBed.setCreateBy(userId);
//	  patientBed.setCreateTime(new Date());
	  patientBed.setCreateTime(LocalDateTime.now());
	  patientBed.setIsHisConfirm("1");
	  Integer insert = baseMapper.insert(patientBed);

	  if (insert > 0) {
		return new Response(200, "床位登记成功");
	  } else {
		return new Response(500, "床位登记失败");
	  }
    }


    /**
     * 转床
     *
     * @param bedId
     * @param patientId
     * @return
     */
    @Override
    public Response changePatientBed1(String bedId, String patientId) {

	  //获取该用户原来床位信息
	  List<PatientBed> patientBedList = baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).eq(PatientBed::getPatientId, patientId)
																	    .eq(PatientBed::getDeleteFlag, "0")
																	    .eq(PatientBed::getIsHisConfirm, "1"));
	  if (CollectionUtils.isEmpty(patientBedList)) {
		return new Response(500, "转床失败");
	  }
	  //判断是否被登记
	  List<PatientBed> parameters = baseMapper.selectList(new LambdaQueryWrapper<>(new PatientBed()).eq(PatientBed::getBedId, bedId)
																	.eq(PatientBed::getIsHisConfirm, "1")
																	.eq(PatientBed::getDeleteFlag, "0"));
	  if (CollectionUtils.isNotEmpty(parameters)) {
		return new Response(500, "转床失败");
	  }
	  //删除原来的登记信息
	  PatientBed patientBed = patientBedList.get(0);
	  patientBed.setDeleteFlag("0");
	  Integer update = baseMapper.updateById(patientBed);
	  Bed bed = bedMapper.selectById(patientBed.getBedId());
	  bed.setIsEmptyCode("0");
	  bed.setIsEmptyName("非空");
	  Integer update1 = bedMapper.updateById(bed);
	  //重新登记新床位
	  String userId = HttpUtil.getRequestHeaderInfo(Constants.USER_ID);
	  //根据病人ID获取就诊号
	  PatientInfo patientInfo = patientInfoMapper.selectById(patientId);
	  PatientBed patientBedNew = new PatientBed();
	  patientBedNew.setId(UUIDUtil.uuid());
	  patientBedNew.setBedId(bedId);
	  patientBedNew.setPatientId(patientId);
//	  patientBedNew.setVisitTypeCode(patientInfo.getVisitTypeCode());
//	  patientBedNew.setVisitTypeName(patientInfo.getVisitTypeName());
//	  patientBedNew.setVisitNo(patientInfo.getVisitNo());
	  patientBedNew.setCreateBy(userId);
//	  patientBed.setCreateTime(new Date());
	  patientBedNew.setCreateTime(LocalDateTime.now());
	  patientBedNew.setIsHisConfirm("1");
	  Integer insert = baseMapper.insert(patientBedNew);
//	  //获取床位信息
//	  Bed bedNew = bedMapper.selectById(bedId);
//	  if (bedNew == null) {
//		return new Response(500, "转床失败");
//	  }
//	  bedNew.setIsEmptyCode("1");
//	  bedNew.setIsEmptyName("非空");
//	  Integer update2 = bedMapper.updateById(bedNew);
	  if (insert > 0 && update > 0 && update1 > 0) {
		return new Response(200, "转床成功");
	  } else {
		return new Response(500, "转床失败");
	  }
    }
}
